//
//  InformationImageCell.m
//  WanCaiC8
//
//  Created by tangang on 2018/4/22.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "InformationImageCell.h"
#import "ImageCell.h"
@interface InformationImageCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic ,strong) UICollectionView *collectionView;
@end

@implementation InformationImageCell{
    QXLable     *labTitle;
    QXLable     *labTime;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        labTitle=[[QXLable alloc]initTitle];
        labTitle.numberOfLines=0;
        labTitle.text=@"008期：王中王解玄机/东方心经/博彩玄机/提供一句解特/特码诗句。";
        [labTitle setSpace:5];
        [self.contentView addSubview:labTitle];
        [labTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).with.offset(11);
            make.left.equalTo(self.contentView).with.offset(10);
            make.right.equalTo(self.contentView).with.offset(-17);
        }];
        
        [self.contentView addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.top.equalTo(labTitle.mas_bottom).with.offset(10);
            make.height.mas_equalTo(@((SCREEN_WIDTH-22)/394.0*160));
        }];
        
        
        labTime=[[QXLable alloc]initWithFount:13];
        [self.contentView addSubview:labTime];
        [labTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labTitle);
            make.bottom.equalTo(self.contentView).with.offset(-12);
            make.top.equalTo(self.collectionView.mas_bottom).with.offset(24);
        }];
        
//        UIImageView *imageV=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"comments"]];
//        [self.contentView addSubview:imageV];
//        [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(labTime.mas_right);
//            make.centerY.equalTo(labTime);
//            make.size.mas_equalTo(imageV.size);
//        }];
        
        UIView *line=[[UIView alloc]init];
        line.backgroundColor=DEFAULTBGCOLOR;
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.contentView);
            make.height.mas_equalTo(@.7);
        }];
    }
    return self;
}

- (void)setModel:(NewsModel *)model{
    _model=model;
    labTitle.text=model.Title;
    labTime.text=[NSString stringWithFormat:@"%@  %@",NEW_APP_NAME,model.ReleaseTime];
    if (self.model.ThumbStyle==2) {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@((SCREEN_WIDTH-22)/394.0*160));
        }];
    }else{
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@((SCREEN_WIDTH-80)/3.0/105*75));
        }];
    }
}

- (void)setAdModel:(AdvertisingModel *)adModel{
    _adModel=adModel;
    if ([_adModel.Title containsString:@"【"]) {
        labTitle.attributedText = [NSAttributedString tg_imageTextWithTitle:_adModel.Title];
        
    } else {
        labTitle.text=_adModel.Title;

    }
    labTime.text=[NSString stringWithFormat:@"%@  %@  %ld  ",adModel.Company,_adModel.SubTime,_adModel.CommentsNumber];
    if (adModel.ThumbStyle==2) {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@((SCREEN_WIDTH-22)/394.0*160));
        }];
    }else{
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@((SCREEN_WIDTH-80)/3.0/105*75));
        }];
    }
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.model) {
        if (self.model.ThumbStyle==2) {
            return 1;
        }
        return self.model.ThumbList.count>3?3:self.model.ThumbList.count;
    }else{
        if (self.adModel.ThumbStyle==2) {
            return 1;
        }
        return self.adModel.ThumbList.count>3?3:self.adModel.ThumbList.count;
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.model.ThumbStyle==2||self.adModel.ThumbStyle==2) {
        return CGSizeMake(SCREEN_WIDTH-22, SCREEN_WIDTH/394.0*160);
    }
    return CGSizeMake((SCREEN_WIDTH-80)/3.0, (SCREEN_WIDTH-80)/3.0/105*75);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"ImageCell" forIndexPath:indexPath];
    if (self.model) {
        if (self.model.ThumbList.count>indexPath.row) {
            [cell.imageV sd_setImageWithURL:self.model.ThumbList[indexPath.row] placeholderImage:[UIImage imageNamed:@"big_loding"]];
        }
    }else{
        if (self.adModel.ThumbList.count>indexPath.row) {
            [cell.imageV sd_setImageWithURL:self.adModel.ThumbList[indexPath.row] placeholderImage:[UIImage imageNamed:@"big_loding"]];
        }
    }
    
    return cell;
    
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 10, 0, 10);
}


#pragma mark -- 懒加载
- (UICollectionView *)collectionView{
    if (_collectionView==nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake((SCREEN_WIDTH-80)/3.0, 60)];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.userInteractionEnabled=NO;
        _collectionView.backgroundColor=[UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator=NO;
        [_collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"ImageCell"];
    }
    return _collectionView;
}

@end
